package cn.com.geoway.booksystem.controller.admin;


import cn.com.geoway.booksystem.entity.Borrow;
import cn.com.geoway.booksystem.entity.User;
import cn.com.geoway.booksystem.service.BookService;
import cn.com.geoway.booksystem.service.BorrowService;

import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/admin/booksearch")
public class Admin_BookSearch_Controller {

    @Resource
    private BorrowService borrowService;


    /**
     * 借阅图书
     */
    @ResponseBody
    @RequestMapping("/borrow")
    public Map<String, Object> borrow(@Valid Borrow borrow, BindingResult bindingResult,
                                      @RequestParam(value = "bookId", required = false) String bookId,
                                      @RequestParam(value = "username", required = false) String username,
                                      HttpServletResponse response, HttpServletRequest request) throws Exception {
        Map<String,Object> map = new HashMap<>();

        if (bindingResult.hasErrors()) {
           map.put("success", false);
           map.put("msg", bindingResult.getFieldError().getDefaultMessage());
           return map;
        } else {
            map = borrowService.borrow(bookId, username);
            return map;
        }
    }
}
